package cn.edu.ctbu.datajpa01.webapi;

import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {
    @Autowired
    StudentService studentService;

    @GetMapping("/user")


    public int user(HttpServletRequest httpServletRequest, String num, String password){

        //判断用户名和密码是否存在
        List<Student> students=studentService.findByNumAndPassword(num,password);




        if (!students.isEmpty()){

            //成功，写入session
            HttpSession session=httpServletRequest.getSession();
            session.setAttribute("userid",students.get(0).getId());

            session.setAttribute("user",students.get(0));

            return 1;
        }else {
            //失败，不存在用户
            return -1;
        }
    }
}
